change size of image and fir it into numpy array opencv

37

# image variable is the image we are manipulating

output_img = np.ones((300, 300, 1)) * 128
output_img = (output_img).astype('uint8')

scale = 300 / (image.shape[0] * 1.0)
image_resize = cv2.resize(image, (0, 0), fx=scale, fy=scale, interpolation=cv2.INTER_LANCZOS4)

img_w = image_resize.shape[1]

if img_w < 300:
    #pad the image with values and make it 1:1 aspect ratio
    offset = img_w % 2
    output_img[:, int(300 / 2 - math.floor(img_w / 2)):int(300 / 2 + math.floor(img_w / 2) + offset), :] = image_resize

else:
    #crop the center of the image to maintain 1:1 aspect ratio
    output_img = image_resize[:,int(img_w / 2 - 300 / 2):
                  int(img_w / 2 + 300 / 2), :]

Comments

Submit
0 Comments